package com.controller;
import com.domain.Book;
import com.entity.PageResult;
import com.service.NewbookProposalService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/proposal")
public class NewbookProposalController {

    @Autowired
    private NewbookProposalService newbookproposalService;

    //显示所有想要的书籍
    @RequestMapping("/searchNewbookProposal")
    public ModelAndView searchNewbookProposal() {
        ModelAndView modelAndView = new ModelAndView();
        //数据
        int pageNum = 1;
        int pageSize = 10;
        PageResult pageResult = newbookproposalService.searchNewbookProposal(pageNum, pageSize);
        modelAndView.addObject("pageResult", pageResult);
        //视图
        modelAndView.setViewName("wantbooks");
        return modelAndView;
    }

    //增加想要书籍
    @ResponseBody
    @RequestMapping("/addproposal")
    public Result addproposal(com.domain.NewbookProposal newbookProposal) {
        try {
            Integer count = newbookproposalService.addNewbookProposal(newbookProposal);
            if (count != 1) {
                return new Result(false, "新书建议推荐失败!");
            }
            return new Result(true, "新书建议推荐成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新书建议推荐失败!");
        }
    }

    //删除想要书籍
    @ResponseBody
    @RequestMapping("/delNewbookProposal")
    public ModelAndView delNewbookProposal(String id) {

        newbookproposalService.delNewbookProposal(id);
        ModelAndView modelAndView = new ModelAndView();
        //数据
        int pageNum = 1;
        int pageSize = 10;
        PageResult pageResult = newbookproposalService.searchNewbookProposal(pageNum, pageSize);
        modelAndView.addObject("pageResult", pageResult);
        //视图
        modelAndView.setViewName("wantbooks");
        return modelAndView;
    }

    //编辑想要书籍，增加
    @ResponseBody
    @RequestMapping("/editNewbookProposal")
    public ModelAndView editNewbookProposal(String id) {
        Integer count=newbookproposalService.editNewbookProposal(id);
        ModelAndView modelAndView = new ModelAndView();
        //数据
        int pageNum = 1;
        int pageSize = 10;
        PageResult pageResult = newbookproposalService.searchNewbookProposal(pageNum, pageSize);
        modelAndView.addObject("pageResult", pageResult);
        //视图
        modelAndView.setViewName("wantbooks");
        return modelAndView;
    }

}
